package com.suoluo.zenadmin.service.hotel;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.common.StringUtil;
import com.suoluo.zenadmin.common.SystemType;
import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.dao.hotel.HotelDAO;
import com.suoluo.zenadmin.oss.OSSService;
import com.suoluo.zenadmin.oss.OSSService.OSSImageException;
import com.suoluo.zenadmin.service.ImageException;
import com.suoluo.zenadmin.service.InfoService;
import com.suoluo.zenadmin.service.ServiceException;
import com.suoluo.zenadmin.service.crawler.CrawlerService;
import com.suoluo.zenadmin.service.crawler.bean.HotelReviewBean;
import com.suoluo.zenadmin.service.crawler.bean.HotelReviewRecordBean;
import com.suoluo.zenadmin.vo.BaseInfoVO;
import com.suoluo.zenadmin.vo.ImageVO;
import com.suoluo.zenadmin.vo.PageVO;
import com.suoluo.zenadmin.vo.hotel.Hotel2VO;
import com.suoluo.zenadmin.vo.hotel.HotelHomeVO;
import com.suoluo.zenadmin.vo.hotel.HotelResource2VO;
import com.suoluo.zenadmin.vo.hotel.HotelReviewRecordVO;
import com.suoluo.zenadmin.vo.hotel.HotelReviewVO;
import com.suoluo.zenadmin.vo.hotel.HotelServicesCategoryVO;
import com.suoluo.zenadmin.vo.hotel.HotelServicesVO;
import com.suoluo.zenadmin.vo.hotel.HotelStyleVO;
import com.suoluo.zenadmin.vo.hotel.HotelTagVO;

public class HotelServiceImpl implements HotelService {

	@Autowired
	private HotelDAO hotelDAO;
	
	@Resource(name="ossService")
	private OSSService ossService;
	
	@Resource(name="infoService")
	private InfoService infoService;
	

	@Override
	public List<HotelServicesCategoryVO> findListHotelServiceCategory() {
		return hotelDAO.findListHotelServiceCategory();
	}

	@Override
	public void saveServiceCategory(HotelServicesCategoryVO serviceVO) {
		String imageName = null;
		try {
			// 保存上传的图片
			imageName = ossService.putImage(serviceVO
					.getIconFile());
			serviceVO.setServiceCategoryOssIcon(imageName);
			// 保存数据到数据库
			hotelDAO.saveHotelServiceCategory(serviceVO);
		} catch (OSSImageException e) {
			throw new ServiceException(CommonResp.FAIL_CODE, "保存图片失败！",e);
		}catch (Exception e) {
			ossService.deleteImage(imageName);
			throw new ServiceException(CommonResp.FAIL_CODE, "酒店基础服务信息保存失败！",e);
		}

	}

	@Override
	public HotelServicesCategoryVO findServiceCategoryById(int serviceId) {
		return hotelDAO.findServiceCategoryById(serviceId);
	}

	@Override
	public void updateHotelServiceCategory(
			HotelServicesCategoryVO serviceVO) {
		String imageName = null;
		try {
			MultipartFile file = serviceVO.getIconFile();
			if (file != null && !file.isEmpty()){
				imageName = ossService.updateImage(file, serviceVO.getServiceCategoryOssIcon());
				serviceVO.setServiceCategoryOssIcon(imageName);
			}
//			FileEntry oldEntry = new FileEntry(serviceVO.getOldImgUrl());
//			// 保存上传的图片
//			entry = UploadImageManager.replaceFile(serviceVO.getIconFile(),
//					oldEntry);
//			if (entry != null) {
//				serviceVO.setServiceCategoryIcon(entry.getFilePath());
//			}
			// 保存数据到数据库
			hotelDAO.updateHotelServiceCategory(serviceVO);
		} catch (OSSImageException e) {
			throw new ServiceException(CommonResp.FAIL_CODE, "保存图片失败！",e);
		}catch (Exception e) {
			ossService.deleteImage(imageName);
			throw new ServiceException(CommonResp.FAIL_CODE, "酒店基础服务信息保存失败！");
		}
	}

	@Override
	public void deleteServiceCategory(HotelServicesCategoryVO serviceVO) {
		int categoryId = serviceVO.getServiceCategoryId();
		// 判断要删除的酒店服务图标是否已被酒店引用，引用不可删除
		List<HotelServicesVO> list = hotelDAO
				.findHotelLinkServiceCategory(categoryId);
		if (!list.isEmpty()) {
			throw new ServiceException(CommonResp.FAIL_CODE,
					"该服务类别已被别的酒店引用，不可以删除！");
		}
		// 删除图片
		ossService.deleteImage(serviceVO.getServiceCategoryOssIcon());
//		FileEntry entry = new FileEntry(serviceVO.getServiceCategoryIcon());
//		UploadImageManager.deleteFile(entry);
		// 删除数据库记录
		hotelDAO.deleteHotelServiceCategory(serviceVO.getServiceCategoryId());
	}

	@Override
	public List<HotelStyleVO> findListHotelStyle() {
		return hotelDAO.findListHotelStyle();
	}

	@Override
	public void saveHotelStyle(HotelStyleVO stylePOJO) {
		hotelDAO.saveHotelStyle(stylePOJO);
	}

	@Override
	public HotelStyleVO findHotelStyleById(int styleId) {
		return hotelDAO.findHotelStyleById(styleId);
	}

	@Override
	public void updateHotelStyle(HotelStyleVO stylePOJO) {
		hotelDAO.updateHotelStyle(stylePOJO);
	}

	@Override
	public void deleteHotelStyle(int styleId) {
		// 先判断该风格是否被其他酒店引用
		int res = hotelDAO.findHotelTag(styleId);
		if (res > 0) {
			throw new ServiceException(CommonResp.FAIL_CODE,
			"该服务类别已被别的酒店引用，不可以删除！");
		}
		// 删除风格信息
		hotelDAO.deleteHotelStyle(styleId);
	}

	@Override
	public List<Hotel2VO> findListHotelByDestId(PageVO page, int destId) {
		int rows = page.getRows();
		int start = PageUtil.calcStartRow(page.getPage(), rows);
		return hotelDAO.findListHotel(start, rows, destId);
	}

	@Override
	public int findHotelCountByDestId(int destId) {
		return hotelDAO.findHotelCountByDestId(destId);
	}

	@Override
	public void saveHotel(Hotel2VO hotelVO) {
		ImageVO image1 = new ImageVO();
		ImageVO image2 = new ImageVO();
		//判断是否选择了酒店提供的服务
		int[] services = hotelVO.getHotelServices();
		if(services == null || services.length == 0) {
			throw new ServiceException(CommonResp.FAIL_CODE, "请选择酒店提供的服务！");
		}
		//判断是否选择了酒店的风格
		int[] styles = hotelVO.getHotelStyles();
		if(styles == null || styles.length == 0) {
			throw new ServiceException(CommonResp.FAIL_CODE, "请选择酒店风格类型！");
		}
		try {
			
			//保存酒店logo
			String logourl = ossService.putImage(hotelVO.getLogoFile());
			hotelVO.setLogo(logourl);
			//设置酒店的浏览次数
			hotelVO.setViewnum(1);
			//保存酒店信息获取酒店Id，设置hotelId，dataId
			int hotelId = hotelDAO.saveHotel(hotelVO);
			hotelVO.setHotelId(hotelId);
			hotelVO.setDataId(hotelId);
			//设置信息类型
			hotelVO.setInfoType(SystemType.INFO_HOTEL);
			//设置默认属性
			hotelVO.setDefaultInfo(SystemType.BIT_TRUE);
			//保存酒店基础信息，获取infoId，设置infoId.
			infoService.saveBaseInfo(hotelVO);
			//保存主图片
			infoService.saveImage(hotelVO);
			//更新酒店图片的imageId
			hotelDAO.updateHotelImageId(hotelVO.getHotelId(),hotelVO.getImageId());
			//保存小图片1
			image1.setInfoId(hotelVO.getInfoId());
			image1.setImagepart(hotelVO.getHfile1());
			infoService.saveImage(image1);
			//保存小图片2
			image2.setInfoId(hotelVO.getInfoId());
			image2.setImagepart(hotelVO.getHfile2());
			infoService.saveImage(image2);

			// 保存基础服务
			List<HotelServicesVO> listService = new ArrayList<HotelServicesVO>();
			
			for (int i = 0; i < services.length; i++) {
				HotelServicesVO ser = new HotelServicesVO();
				ser.setHotelId(hotelId);
				ser.setServiceCategoryId(services[i]);

				listService.add(ser);
			}
			hotelDAO.saveHotelServices(listService);

			// 保存风格
			List<HotelTagVO> listStyle = new ArrayList<HotelTagVO>();
			
			for (int i = 0; i < styles.length; i++) {
				HotelTagVO ser = new HotelTagVO();
				ser.setHotelId(hotelId);
				ser.setStyleId(styles[i]);

				listStyle.add(ser);
			}
			hotelDAO.saveHotelStyles(listStyle);

			
		}catch (ImageException e) {
			throw e;
		} catch (Exception e) {
			ossService.deleteImage(hotelVO.getSource());
			ossService.deleteImage(image1.getSource());
			ossService.deleteImage(image2.getSource());
			ossService.deleteImage(hotelVO.getLogo());
			throw new ServiceException(CommonResp.FAIL_CODE, "酒店信息保存失败！",e);
		}

	}

	@Override
	public Hotel2VO findSimplestHotelById(int hotelId) {
		Hotel2VO hotel =  hotelDAO.findSimplestHotelById(hotelId);
		hotel.setName(StringUtil.buildCompleteName(hotel.getZhname(), hotel.getEnname()));
		return hotel;
	}
	
	@Override
	public Hotel2VO findHotelById(int hotelId) {
		Hotel2VO hotel = hotelDAO.findHotelWithoutImageById(hotelId, SystemType.INFO_HOTEL);
		hotel.setName(StringUtil.buildCompleteName(hotel.getZhname(), hotel.getEnname()));
		List<ImageVO> images = infoService.findImages(hotel.getInfoId());
		int c = 0;
		for (ImageVO i : images){
			if (i.getImageId() == hotel.getImageId()){
				hotel.setSource(i.getSource());
				hotel.setSmall(i.getSmall());
				hotel.setMiddle(i.getMiddle());
				hotel.setNormal(i.getNormal());
			} else {
				if (c == 0){
					hotel.setImage1(i);
					c++;
				} else {
					hotel.setImage2(i);
				}
			}
		}
		return hotel;
	}

	@Override
	public void updateHotel(Hotel2VO hotelVO) {
		ImageVO image1 = new ImageVO();
		ImageVO image2 = new ImageVO();
		MultipartFile hfile1 = hotelVO.getHfile1();
		MultipartFile hfile2 = hotelVO.getHfile2();
		MultipartFile source = hotelVO.getImagepart();
		MultipartFile logoFile = hotelVO.getLogoFile();
		try {
			//判断logo是否修改
			if(logoFile != null && !logoFile.isEmpty())
			{
				String logoName = ossService.updateImage(logoFile, hotelVO.getLogo());
				hotelVO.setLogo(logoName);
			}
			//修改酒店信息
			hotelDAO.updateHotel(hotelVO);
			//修改基础信息
			infoService.updateBaseInfo(hotelVO);
			//判断主图片是否修改
			if(source != null && !source.isEmpty())
			{
				infoService.updateImage(hotelVO);
			}
			//判断小图片1是否修改
			if(hfile1 != null && !hfile1.isEmpty())
			{
				image1.setSource(hotelVO.getImage1().getSource());
				image1.setImagepart(hfile1);
				image1.setImageId(hotelVO.getImage1().getImageId());
				infoService.updateImage(image1);
			}
			//判断小图片2是否修改
			if(hfile2 != null && !hfile2.isEmpty())
			{
				image2.setSource(hotelVO.getImage2().getSource());
				image2.setImagepart(hfile2);
				image2.setImageId(hotelVO.getImage2().getImageId());
				infoService.updateImage(image2);
			}
			
			//同步酒店信息到目的地特色项表
			hotelDAO.synHotelToFeatureItem(hotelVO);

		}catch (ImageException e) {
			throw e;
		} catch (Exception e) {
			if(logoFile != null && !logoFile.isEmpty())
			{
				ossService.deleteImage(hotelVO.getLogo());
			}
			if(source != null && !source.isEmpty())
			{
				ossService.deleteImage(hotelVO.getSource());
			}
			if(hfile1 != null && !hfile1.isEmpty())
			{
				ossService.deleteImage(image1.getSource());
			}
			if(hfile2 != null && !hfile2.isEmpty())
			{
				ossService.deleteImage(image2.getSource());
			}
			throw new ServiceException(CommonResp.FAIL_CODE, "酒店信息保存失败！",e);
		}
	}

	@Override
	public void deleteHotel(Hotel2VO hotelVO) {
		int hotelId = hotelVO.getHotelId();
		hotelVO.setDataId(hotelId);
		hotelVO.setInfoType(SystemType.INFO_HOTEL);
		int hotelForeignKeyCount = hotelDAO.findHotelForeignKeyCount(hotelId);
		if (hotelForeignKeyCount > 0) {
			throw new ServiceException(CommonResp.FAIL_CODE, "酒店已关联资源，不可以删除！");
		}

		// 删除酒店5条评论信息
		hotelDAO.deleteHotelReviewRecord(hotelId);
		// 删除酒店评论统计信息
		hotelDAO.deleteHotelReview(hotelId);
		// 删除酒店提供的服务信息
		hotelDAO.deleteHotelServices(hotelId);
		// 删除酒店的风格信息
		hotelDAO.deleteHotelAndStyle(hotelId);
		//防止该酒店已被勾选为推荐项，先删除推荐项
		Integer itemId = hotelDAO.findDestFeatureItemId(hotelId);
		if(itemId != null && itemId != 0)
		{
			hotelDAO.deleteSelectFeatureItem(itemId);
		}
		// 如果酒店关联到目的地的特色项，则删除该特色项
		hotelDAO.deleteDestFeatureItem(hotelId);
		// 删除酒店信息
		infoService.deleteBaseInfo(hotelVO);
		hotelDAO.deleteHotel(hotelId);
		// 删除酒店的logo
		ossService.deleteImage(hotelVO.getLogo());
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findListHotelAndServices(int hotelId) {
		return hotelDAO.findListHotelAndServices(hotelId);
	}

	@Override
	public void updateHotelAndService(Hotel2VO hotelVO) {
		int[] services = hotelVO.getHotelServices();
		if(services == null || services.length == 0) {
			throw new ServiceException(CommonResp.FAIL_CODE, "请选择酒店提供的服务！");
		}
		// 先删除原有关联的服务信息
		int hotelId = hotelVO.getHotelId();
		hotelDAO.deleteHotelServices(hotelId);
		// 保存基础服务
		List<HotelServicesVO> listService = new ArrayList<HotelServicesVO>();
		
		for (int i = 0; i < services.length; i++) {
			HotelServicesVO ser = new HotelServicesVO();
			ser.setHotelId(hotelId);
			ser.setServiceCategoryId(services[i]);

			listService.add(ser);
		}
		// 在添加关联的服务信息
		hotelDAO.saveHotelServices(listService);

	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findListHotelAndStyles(int hotelId) {
		return hotelDAO.findListHotelAndStyles(hotelId);
	}

	@Override
	public void updateHotelAndStryles(Hotel2VO hotelVO) {
		int[] styles = hotelVO.getHotelStyles();
		if(styles == null || styles.length == 0) {
			throw new ServiceException(CommonResp.FAIL_CODE, "请选择酒店风格类型！");
		}
		// 删除原有的风格信息
		int hotelId = hotelVO.getHotelId();
		hotelDAO.deleteHotelAndStyle(hotelId);
		// 保存风格
		List<HotelTagVO> listStyle = new ArrayList<HotelTagVO>();
		
		for (int i = 0; i < styles.length; i++) {
			HotelTagVO ser = new HotelTagVO();
			ser.setHotelId(hotelId);
			ser.setStyleId(styles[i]);

			listStyle.add(ser);
		}
		hotelDAO.saveHotelStyles(listStyle);
	}

	@Override
	public List<HotelResource2VO> findHotelResourcesWithoutContentByHotelId(int hotelId) {

		List<HotelResource2VO> resources =  hotelDAO.findHotelResourcesWithoutContentByHotelId(hotelId);
		for (HotelResource2VO r : resources){
			r.setName(StringUtil.buildCompleteName(r.getZhname(), r.getEnname()));
		}
		return resources;
	}

	@Override
	public void saveHotelResource(HotelResource2VO resource) {
		List<String> imageNames = null;
		try {
			//保存酒店资源项信息
			int resourceId = hotelDAO.saveHotelResource(resource);
			resource.setDataId(resourceId);
			resource.setResourceId(resourceId);
			//设置数据类型
			resource.setInfoType(SystemType.INFO_HOTEL_RESOURCE);
			//设置默认信息
			resource.setDefaultInfo(SystemType.BIT_TRUE);
			//保存基础信息
			infoService.saveBaseInfo(resource);
			
			//保存图片信息
			imageNames = ossService.putImages(resource.getMfile());
			List<Integer> list = infoService.saveImage(resource.getInfoId(), imageNames);
			//更新图片Id
			hotelDAO.updateResourceRelImg(resourceId, list.get(0));
			
		}catch (ImageException e) {
			throw e;
		} catch (Exception e) {
//			ossService.deleteImages(imageNames);
			throw new ServiceException(CommonResp.SUCCESS_CODE, "酒店资源和图片保存失败！",e);
		}
	}

	@Override
	public HotelResource2VO findHotelResourceById(int resourceId) {
		return hotelDAO.findHotelResourceById(resourceId,SystemType.INFO_HOTEL_RESOURCE);
	}

	@Override
	public void updateHotelResource(HotelResource2VO resource) {		
		// 更新资源数据
		hotelDAO.updateHotelResource(resource);
		//更新基础信息
		infoService.updateBaseInfo(resource);
	}

	@Override
	public void deleteHotelResource(HotelResource2VO resource) {
		int resourceId = resource.getResourceId();
		resource.setDataId(resourceId);
		resource.setInfoType(SystemType.INFO_HOTEL_RESOURCE);
		//删除酒店资源信息
		hotelDAO.deleteHotelResource(resourceId);
		resource.setDataId(resourceId);
		//删除酒店资源基础信息
		infoService.deleteBaseInfo(resource);
		
	}


//	@Override
//	public void savePickDefaultImg(int resourceId, int imageId) {
//		hotelDAO.updateResourceRelImg(resourceId, imageId);
//	}

//	@Override
//	public List<HotelResourceItemPOJO> findListHotelResourceItem(int resourceId) {
//		return hotelDAO.findListHotelResourceItem(resourceId);
//	}

	@Override
	public void saveHotelResourceItem(HotelResource2VO resource) {
		List<String> imageNames = null;
		try {

			imageNames = ossService.putImages(resource.getMfile());
			if (imageNames == null || imageNames.isEmpty()){
				throw new ServiceException(CommonResp.SUCCESS_CODE, "酒店资源图片保存失败！"); 
			}
			// 将图片信息保存到酒店资源项表
			infoService.saveImage(resource.getInfoId(), imageNames);
		}catch (ImageException e) {
			throw e;
		}catch (Exception e) {
//			ossService.deleteImages(imageNames);
			throw new ServiceException(CommonResp.SUCCESS_CODE, "酒店资源图片保存失败！",e);
		}

	}

	@Override
	public void deleteHotelResourceItem(ImageVO image) {
		infoService.deleteImage(image);
	}

	@Override
	public List<HotelReviewVO> findListHotelReview(int hotelId) {
		return hotelDAO.findListHotelReview(hotelId);
	}

	@Override
	public void saveHotelReview(HotelReviewVO review) {
		Date date = new Date();
		review.setCrawlerTime(date);
		int reviewType = review.getReviewType();
		int res = hotelDAO.findHotelReviewByReviewType(review.getHotelId(),
				reviewType);
		if (res > 0) {
			throw new ServiceException(CommonResp.FAIL_CODE,
					"添加失败，因为该酒店已经抓取了该网页数据！");
		}
		String reviewUrl = review.getReviewUrl();
		// 根据reviewUrl中的域名做判断，符合要求的才能添加
		Map<Integer, String> map = SystemConfigHolder.getHotelReviewType();
		int index = reviewUrl.indexOf(map.get(reviewType));
		if (index != -1) {
			int reviewId = hotelDAO.saveHotelReview(review);
			review.setReviewId(reviewId);
			// 抓取评论信息
			CrawlerService.getInstance().crawlerHotelReview(review);
		} else {
			throw new ServiceException(CommonResp.FAIL_CODE, "添加失败,网页抓取类型要为:"
					+ map.get(reviewType));
		}

	}

	@Override
	public HotelReviewVO findHotelReviewById(int reviewId) {
		return hotelDAO.findHotelReviewById(reviewId);
	}

	@Override
	public void updateHotelReview(HotelReviewVO review) {
		int reviewType = review.getReviewType();
		String reviewUrl = review.getReviewUrl();
		// 根据reviewUrl中的域名做判断，符合要求的才能添加
		Map<Integer, String> map = SystemConfigHolder.getHotelReviewType();
		int index = reviewUrl.indexOf(map.get(reviewType));
		if (index != -1) {
			hotelDAO.updateHotelReview(review);
			// 抓取评论信息
			CrawlerService.getInstance().crawlerHotelReview(review);
		} else {
			throw new ServiceException(CommonResp.FAIL_CODE, "添加失败,网页抓取类型要为:"
					+ map.get(reviewType));
		}

	}

	@Override
	public void deleteHotelReview(int reviewId) {
		// 根据reviewId删除评论信息
		hotelDAO.deleteHotelReviewRecordByReviewId(reviewId);
		// 根据reviewId删除评论统计信息
		hotelDAO.deleteHotelReviewByReviewId(reviewId);
	}

	@Override
	public List<HotelReviewRecordVO> findListHotelReviewRecord(int reviewId) {
		return hotelDAO.findListHotelReviewRecord(reviewId);
	}

	@Override
	public List<HotelReviewVO> findInterruptReview(int crawlerStatus) {
		return hotelDAO.findInterruptReview(crawlerStatus);
	}

	@Override
	public List<HotelReviewVO> findExpireReview(int holdDays) {
		return hotelDAO.findExpireReview(holdDays);
	}

	@Override
	public void updateCrawlerState(int status, int reviewId) {
		hotelDAO.updateCrawlerState(status, reviewId);
	}

	@Override
	public void updateIncrementFailCountAndSetFail(int status, int reviewId) {
		hotelDAO.updateIncrementFailCountAndSetFail(status, reviewId);
	}

	@Override
	public void updateCrawlerReviewInfo(HotelReviewBean review) {
		
		hotelDAO.updateCrawlerReviewInfo(review);
		int reviewId = review.getReviewId();
		// 根据reviewId删除评论表tb_hotel_review_record中数据
		hotelDAO.deleteHotelReviewRecordByReviewId(reviewId);
		// 向表tb_hotel_review_record 插入新数据
		List<HotelReviewRecordVO> listRecord = new ArrayList<HotelReviewRecordVO>();
		List<HotelReviewRecordBean> list = review.getReviewRecords();
		if(list == null || list.isEmpty())
		{
			return;
		}
		for (HotelReviewRecordBean hotelReviewRecordBean : list) {
			HotelReviewRecordVO hotelReviewRecord = toChangeHotelReviewRecordBean(hotelReviewRecordBean);
			hotelReviewRecord.setReviewId(reviewId);
			String reviewRecordDesc = hotelReviewRecord.getRecordDesc();
			//过滤掉四个字节的字符
			try {
				reviewRecordDesc = StringUtil.filterOffUtf8Mb4(reviewRecordDesc);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			//描述字段可能操作512个字符
			if (reviewRecordDesc.length() > 512){
				hotelReviewRecord.setRecordDesc(reviewRecordDesc.substring(0, 512));
			}else{
				hotelReviewRecord.setRecordDesc(reviewRecordDesc);
			}
			String reviewRecordTitle = hotelReviewRecord.getRecordTitle();
			if (reviewRecordTitle.length() > 64){
				hotelReviewRecord.setRecordTitle(reviewRecordTitle.substring(0,64));
			}
			listRecord.add(hotelReviewRecord);
		}
		
		hotelDAO.saveHotelReviewRecord(listRecord);
	}
	/**
	 * 转换HotelReviewRecordBean为HotelReviewRecordVO
	 * 
	 * @param hotelReviewRecordBean
	 * @return
	 */
	public HotelReviewRecordVO toChangeHotelReviewRecordBean(
			HotelReviewRecordBean hotelReviewRecordBean) {
		HotelReviewRecordVO hotelReviewRecord = new HotelReviewRecordVO();
		hotelReviewRecord
				.setRecordTitle(hotelReviewRecordBean.getRecordTitle());
		hotelReviewRecord.setRecordDesc(hotelReviewRecordBean.getRecordDesc());
		hotelReviewRecord
				.setRecordAutor(hotelReviewRecordBean.getRecordAutor());
		hotelReviewRecord.setRecordTime(hotelReviewRecordBean.getRecordTime());
		hotelReviewRecord
				.setRecordScore(hotelReviewRecordBean.getRecordScore());
		hotelReviewRecord.setRecordFullScore(hotelReviewRecordBean
				.getRecordFullScore());
		return hotelReviewRecord;
	}

	@Override
	public List<Hotel2VO> findHotelsWithoutContentByDestId(int destId) {
		List<Hotel2VO> hotels = hotelDAO.findHotelsWithoutContentByDestId(destId);
		for (Hotel2VO h : hotels){
			h.setName(StringUtil.buildCompleteName(h.getZhname(), h.getEnname()));
		}
		return hotels;
	}

	@Override
	public HotelHomeVO findHotelHomeData(int hotelId) {
		HotelHomeVO hotelHomeVO = new HotelHomeVO();
		//1.查询酒店基础信息
		Hotel2VO hotel = this.findHotelById(hotelId);
		String hotelDesc = UBB.decode(hotel.getContent());
		hotel.setContent(hotelDesc);
		hotelHomeVO.setHotel(hotel);
		String location = UBB.decode(hotel.getLocation());
		hotel.setLocation(location);
		String special = UBB.decode(hotel.getSpecial());
		hotel.setSpecial(special);
		String honor = UBB.decode(hotel.getHonor());
		hotel.setHonor(honor);
		
		//2.酒店的服务图标信息
		List<HotelServicesCategoryVO> categorys = hotelDAO.findHotelServicesByHotelId(hotelId);
		hotelHomeVO.setServiceCategorys(categorys);
		
		//3.查询酒店相关的四个酒店
		List<Hotel2VO> samilarHotels = hotelDAO.findSamilarHotels(hotel,SystemConfigHolder.getRecommendSize());
		if (samilarHotels == null || samilarHotels.isEmpty()){
			samilarHotels = hotelDAO.findSamilarHotels(hotel.getHotelId(),hotel.getStyleId(),SystemConfigHolder.getRecommendSize());
		}
		for (Hotel2VO h : samilarHotels){
			h.setName(StringUtil.buildCompleteName(h.getZhname(), h.getEnname()));
		}
		hotelHomeVO.setSamilarHotels(samilarHotels);
		
		//4.查询酒店的风格
		List<HotelStyleVO> styles = hotelDAO.findHotelStyles(hotelId);
		StringBuilder styleInfo = new StringBuilder();
		StringBuilder strStyleInfo = new StringBuilder();
		for (HotelStyleVO hotelStyle : styles) {
			styleInfo.append(hotelStyle.getStyleName() + " ");
			strStyleInfo.append(hotelStyle.getStyleName() + ",");
		}
		hotelHomeVO.setStyle(styleInfo.toString());
		hotelHomeVO.setStyleKeywords(strStyleInfo.toString());
		
		return hotelHomeVO;
	}
	
	@Override
	public List<HotelResource2VO> findHotelResourcesWithoutContentByHotelId(int hotelId,
			int resourceType) {
		List<HotelResource2VO> resources =  hotelDAO.findHotelResourcesWithoutContentByHotelId(hotelId, resourceType);
		for (HotelResource2VO r : resources){
			r.setName(StringUtil.buildCompleteName(r.getZhname(), r.getEnname()));
		}
		return resources;
	}
	
	@Override
	public HotelResource2VO findSimplestHotelResourceById(int resourceId) {
		HotelResource2VO r = hotelDAO.findSimplestHotelResourceById(resourceId);
		r.setName(StringUtil.buildCompleteName(r.getZhname(), r.getEnname()));
		return r;
	}

	@Override
	public HotelResource2VO findHotelResourceAndItems(HotelResource2VO resource) {
//		HotelResource2VO resource = hotelDAO.findHotelResourceWithoutImageById(reosurceId, );
		List<BaseInfoVO> infos = infoService.findBaseInfos(resource.getResourceId(), SystemType.INFO_HOTEL_RESOURCE);
		if (infos != null && !infos.isEmpty()){
			BaseInfoVO baseInfo = infos.get(0);
			resource.setContent(UBB.decode(baseInfo.getContent()));
			resource.setInfoId(baseInfo.getInfoId());
			resource.setInfoType(SystemType.INFO_HOTEL_RESOURCE);
			List<ImageVO> images = infoService.findImages(baseInfo.getInfoId());
			resource.setImages(images);
		}
		return resource;
	}
	
//	@Override
//	public HotelAroundVO findHotelAroundData(int hotelId, int destId,
//			int curCategoryId) {
//		List<Map> nodes = hotelDAO.findHotelAroundData(destId);
////		String centerHotelUrlSuffix = "hotelId=" + hotelId;
//		Map<Integer,AroundCategoryVO> categoryMap = new HashMap<Integer, AroundCategoryVO>();
//		HotelAroundVO vo = new HotelAroundVO();
//		for (Map row : nodes){
//			
//			BigDecimal lat = (BigDecimal)row.get("lat");
//			BigDecimal lng =  (BigDecimal)row.get("lng");
//			if (lat == null || lng == null){
//				continue;
//			}
//			AroundNodeVO node = new AroundNodeVO();
//			node.setNodeId((Integer) row.get("feature_item_id"));
//			String zhname = (String) row.get("zhname");
//			String enname = (String) row.get("enname");
//			node.setNodeName(StringUtil.buildCompleteName(zhname, enname));
//			Integer relId = (Integer) row.get("rel_id");
//			node.setRelId(relId);
//			node.setLat(lat);
//			node.setLng(lng);
//			String source = (String) row.get("source");
//			String small = (String) row.get("small");
//			node.setImage(source + small);
////			node.setOrigImg((String) row.get("t_feature_item_orig_img"));
////			node.setMiniImg((String) row.get("t_feature_item_mini_img"));
//			node.setDestId(destId);
//			
//			//如果是酒店类型，酒店类型的nodeurl = /views/hotel/hotel.do?action=viewHotelDetail&hotelId=14
//			//所以根据hotelId=14l来验证是否是中心节点酒店
//			if (relId != null && relId == hotelId){
//				vo.setCenter(node);
//			}
//			else{
//				Integer categoryId = (Integer) row.get("t_feature_category_id");
//				AroundCategoryVO category = categoryMap.get(categoryId);
//				if (category == null){
//					category = new AroundCategoryVO();
//					category.setCategoryId(categoryId);
//					category.setCategoryName((String) row.get("t_feature_category_name"));
//					category.setCategoryOrder((Integer) row.get("t_feature_category_order"));
//					category.setHotelId(hotelId);
//					category.setDestId(destId);
//					category.setCategoryColor((String) row.get("t_feature_category_color"));
//					if (categoryId == curCategoryId){
//						vo.setCurCategory(category);
//					}
//					categoryMap.put(categoryId, category);
//					
//				}
//				category.addAroundNodeVO(node);
//			}
//		}
//		if (vo.getCenter() == null){
//			return new HotelAroundVO();
//		}
//		vo.setCategoryList(new ArrayList<AroundCategoryVO>(categoryMap.values()));
//		vo.calcDistanceAndSort();
//		return vo;
//	}

	@Override
	public void updateDefaultImage(int resourceId, int imageId) {
		hotelDAO.updateResourceRelImg(resourceId, imageId);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Hotel2VO> findNewHotels(int curPage) {
		int pageRows = 12;
		int startPos = PageUtil.calcStartRow(curPage, pageRows);
		List<Hotel2VO> hotels = hotelDAO.findNewHotels(startPos, pageRows);
		for (Hotel2VO h : hotels){
			h.setName(StringUtil.buildCompleteName(h.getZhname(), h.getEnname()));
		}
		return hotels;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Hotel2VO> findHotHotels(int curPage) {
		int pageRows = 12;
		int startPos = PageUtil.calcStartRow(curPage, pageRows);
		List<Hotel2VO> hotels = hotelDAO.findHotHotels(startPos, pageRows);
		for (Hotel2VO h : hotels){
			h.setName(StringUtil.buildCompleteName(h.getZhname(), h.getEnname()));
		}
		return hotels;
	}

	@Override
	public void saveHotelText(BaseInfoVO info) {
		try{
			//设置数据类型
			info.setInfoType(SystemType.INFO_HOTEL);
			//设置属性为非默认
			info.setDefaultInfo(SystemType.BIT_FALSE);
			//保存基础信息,获取infoId
			infoService.saveBaseInfo(info);
			//保存图片信息
			List<String> list = ossService.putImages(info.getImageFiles());
			infoService.saveImage(info.getInfoId(),list);
		}catch (ImageException e) {
			throw e;
		}
		catch (Exception e) {
			ossService.deleteImage(info.getSource());
			throw new ServiceException(CommonResp.FAIL_CODE,"酒店图文保存失败！",e);
		}
		
	}

	@Override
	public void saveHotelImage(BaseInfoVO info) {
		try{
			//保存图片信息
			List<String> list = ossService.putImages(info.getImageFiles());
			infoService.saveImage(info.getInfoId(),list);
		}catch (ImageException e) {
			throw e;
		}
		catch (Exception e) {
			ossService.deleteImage(info.getSource());
			throw new ServiceException(CommonResp.FAIL_CODE,"酒店多图保存失败！",e);
		}
	}

	@Override
	public List<BaseInfoVO> findHotelInfoAndImages(int hotelId) {
		
		List<BaseInfoVO> listb = infoService.findBaseInfosNoDefault(hotelId, SystemType.INFO_HOTEL);	
		
		
		Map<Integer,BaseInfoVO> subItemMap = new HashMap<Integer, BaseInfoVO>();
		int [] infoIds = new int[listb.size()];
		int i = 0;
		for (BaseInfoVO sub : listb){
			subItemMap.put(sub.getInfoId(), sub);
			infoIds[i++] = sub.getInfoId();
		}
		List<ImageVO> images = infoService.findImages(infoIds);
		for (ImageVO img : images){
			BaseInfoVO info = subItemMap.get(img.getInfoId());
			info.addImage(img);
		}
		
		return listb;
	}

	@Override
	public List<Hotel2VO> findAllHotel(int destId) {
		return hotelDAO.findAllHotel(destId);
	}

	@Override
	public String findHotelStyle(int hotelId) {
		List<HotelStyleVO> list = hotelDAO.findHotelStyles(hotelId);
		String styles = null;
		for (HotelStyleVO style : list) {
			if(styles == null){
				styles = style.getStyleName();
			}
			else
			{
				styles = styles + " " + style.getStyleName();
			}
			
		}
		return styles;
	}

	@Override
	public List<HotelResource2VO> findHotelResources(int hotelId) {
		return hotelDAO.findHotelResources(hotelId, SystemType.INFO_HOTEL_RESOURCE);
	}

}
